<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(B卷,100分)- 构成正方形的数量（Java & JS & Python）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>输入N个互不相同的二维整数坐标&#xff0c;求这N个坐标可以构成的正方形数量。[内积为零的的两个向量垂直]</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>第一行输入为N&#xff0c;N代表坐标数量&#xff0c;N为正整数。N &lt;&#61; 100</p> 
<p>之后的 K 行输入为坐标x y以空格分隔&#xff0c;x&#xff0c;y为整数&#xff0c;-10&lt;&#61;x, y&lt;&#61;10</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>输出可以构成的正方形数量。</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:71px;">输入</td><td style="width:427px;"> <p>3<br /> 1 3<br /> 2 4<br /> 3 1</p> </td></tr><tr><td style="width:71px;">输出</td><td style="width:427px;">0 &#xff08;3个点不足以构成正方形&#xff09;</td></tr><tr><td style="width:71px;">说明</td><td style="width:427px;">无</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:74px;">输入</td><td style="width:424px;"> <p>4<br /> 0 0<br /> 1 2<br /> 3 1<br /> 2 -1</p> </td></tr><tr><td style="width:74px;">输出</td><td style="width:424px;">1</td></tr><tr><td style="width:74px;">说明</td><td style="width:424px;">无</td></tr></tbody></table> 
<h3 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90"></h3> 
<h4 id="%E6%95%B0%E5%AD%A6%E6%96%B9%E6%B3%95%E6%B1%82%E8%A7%A3%E6%9C%AC%E9%A2%98">题目解析</h4> 
<p>其实当我们知道正方形相邻两点的坐标&#xff0c;即某条边的坐标后&#xff0c;就可以求出其余两点的坐标。</p> 
<p>如下图中&#xff0c;我们知道正方形的红色点坐标后&#xff0c;就画出绿色点坐标和橙色点坐标来形成两个正方形&#xff0c;这其中似乎隐藏着什么规律&#xff1f;</p> 
<p><img alt="" height="550" src="https://img-blog.csdnimg.cn/45b4dfe963f5472eb73a7de4c8c724e6.png" width="861" /></p> 
<p>我们选取其中一个正方形来分析</p> 
<p><img alt="" height="498" src="https://img-blog.csdnimg.cn/05ec54c1186e41f5af22dda6f3a98dc8.png" width="654" /></p> 
<p>我们在目标正方形外面包裹一个更大的正方形&#xff0c;此时可以发现大正方形和小正方形相交点切割出了相同的几个尺寸&#xff1a;d1和d2。</p> 
<p></p> 
<p>假设已知A点坐标(x1, y1)&#xff0c;B点坐标(x2,y2)&#xff0c;那么</p> 
<ul><li>d1 &#61; x1 - x2</li><li>d2 &#61; y1 - y2</li></ul> 
<p>其实很容易可以发现&#xff0c;d1含义是A&#xff0c;B两点横向距离&#xff0c;d2是A&#xff0c;B两点纵向距离。</p> 
<p></p> 
<p>基于A,B点坐标&#xff0c;以及d1&#xff0c;d2&#xff0c;我们可以算出C,D点坐标分别为&#xff1a;</p> 
<ul><li>C坐标 &#xff08;x2 &#43; d2, y2 - d1&#xff09;</li><li>D坐标 &#xff08;x1 &#43; d2, y1 - d1&#xff09;</li></ul> 
<p>继续转化一下可得&#xff1a;</p> 
<ul><li>C坐标 &#xff08;x2 &#43; y1 - y2, y2 - &#xff08;x1 - x2&#xff09;&#xff09;</li><li>D坐标 &#xff08;x1 &#43; y1 - y2, y1 - &#xff08;x1 - x2&#xff09;&#xff09;</li></ul> 
<p>这是求A&#xff0c;B右下方向C&#xff0c;D边得公式推导。</p> 
<p></p> 
<p>同理&#xff0c;可以根据A&#xff0c;B推导出其左上方向E&#xff0c;F边&#xff0c;图示如下&#xff1a;</p> 
<p><img alt="" height="459" src="https://img-blog.csdnimg.cn/b5af60d07d36444dbfc31ffb2f8ea304.png" width="635" /></p> 
<p>基于A,B点坐标&#xff0c;以及d1&#xff0c;d2&#xff0c;我们可以算出E,F点坐标分别为&#xff1a; </p> 
<ul><li>E坐标 &#xff08;x1 - d2&#xff0c;y1 &#43; d1&#xff09;</li><li>F坐标 &#xff08;x2 - d2&#xff0c;y1 &#43; d1&#xff09;</li></ul> 
<p>继续转化一下可得&#xff1a;</p> 
<ul><li>E坐标 &#xff08;x1 - &#xff08;y1 - y2&#xff09;&#xff0c;y1 &#43; x1 - x2&#xff09;</li><li>F坐标 &#xff08;x2 - &#xff08;y1 - y2&#xff09;&#xff0c;y1 &#43; x1 - x2&#xff09;</li></ul> 
<p></p> 
<p>此时我们就得到了根据正方形任意相邻两点坐标&#xff0c;求另外两点坐标的公式了。</p> 
<p>因此&#xff0c;接下来我们只需要遍历出两个点&#xff0c;然后通过公式得出另外可能的两个点&#xff0c;再在所有点中查找是否存在可能的两点&#xff0c;若存在&#xff0c;则正方形count&#43;&#43;。</p> 
<p></p> 
<p>最后的正方形个数squareCount 需要除以 4&#xff0c;原因是&#xff0c;如果输入中真的存在如下图中的绿色&#xff0c;橙色点&#xff0c;则遍历过程中也会将绿色&#xff0c;橙色点遍历出来&#xff0c;然后求它们的可能正方形</p> 
<p><img alt="" height="550" src="https://img-blog.csdnimg.cn/45b4dfe963f5472eb73a7de4c8c724e6.png" width="861" /></p> 
<p>也就是说上图中两个正方形&#xff0c;不仅会被两个红色点求出来两次次&#xff0c;还会被两个绿色点求出来一次&#xff0c;还会被两个橙色点求出来一次&#xff0c;还会被一绿一红求出来两次&#xff0c;被一橙一红求出来两次 &#xff0c;总共是8次&#xff0c;而实际上只有2个正方形&#xff0c;因此最终结果要除以4。</p> 
<p></p> 
<h4>JavaScript算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const lines &#61; [];
let n;
rl.on(&#34;line&#34;, (line) &#61;&gt; {
  lines.push(line);

  if (lines.length &#61;&#61;&#61; 1) {
    n &#61; parseInt(lines[0]);
  }

  if (n &amp;&amp; lines.length &#61;&#61;&#61; n &#43; 1) {
    lines.shift();

    console.log(getSquareCount(lines));

    lines.length &#61; 0;
  }
});

/* 数学公式验证正方形 */
function getSquareCount(coordinates) {
  let squareCount &#61; 0;

  const set &#61; new Set(coordinates);

  for (let i &#61; 0; i &lt; coordinates.length; i&#43;&#43;) {
    let [x1, y1] &#61; coordinates[i].split(&#34; &#34;).map((ele) &#61;&gt; parseInt(ele));

    for (let j &#61; i &#43; 1; j &lt; coordinates.length; j&#43;&#43;) {
      let [x2, y2] &#61; coordinates[j].split(&#34; &#34;).map((ele) &#61;&gt; parseInt(ele));

      let x3 &#61; x1 - (y1 - y2);
      let y3 &#61; y1 &#43; (x1 - x2);
      let x4 &#61; x2 - (y1 - y2);
      let y4 &#61; y2 &#43; (x1 - x2);
      if (set.has(x3 &#43; &#34; &#34; &#43; y3) &amp;&amp; set.has(x4 &#43; &#34; &#34; &#43; y4)) squareCount&#43;&#43;;

      let x5 &#61; x1 &#43; (y1 - y2);
      let y5 &#61; y1 - (x1 - x2);
      let x6 &#61; x2 &#43; (y1 - y2);
      let y6 &#61; y2 - (x1 - x2);
      if (set.has(x5 &#43; &#34; &#34; &#43; y5) &amp;&amp; set.has(x6 &#43; &#34; &#34; &#43; y6)) squareCount&#43;&#43;;
    }
  }

  return squareCount / 4;
}
</code></pre> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    int n &#61; Integer.parseInt(sc.nextLine());

    String[] coordinates &#61; new String[n];
    for (int i &#61; 0; i &lt; n; i&#43;&#43;) {
      coordinates[i] &#61; sc.nextLine();
    }

    System.out.println(getResult(n, coordinates));
  }

  public static int getResult(int n, String[] coordinates) {
    int squareCount &#61; 0;

    HashSet&lt;String&gt; set &#61; new HashSet&lt;&gt;(Arrays.asList(coordinates));

    for (int i &#61; 0; i &lt; n; i&#43;&#43;) {
      Integer[] arr1 &#61;
          Arrays.stream(coordinates[i].split(&#34; &#34;)).map(Integer::parseInt).toArray(Integer[]::new);
      int x1 &#61; arr1[0], y1 &#61; arr1[1];

      for (int j &#61; i &#43; 1; j &lt; n; j&#43;&#43;) {
        Integer[] arr2 &#61;
            Arrays.stream(coordinates[j].split(&#34; &#34;)).map(Integer::parseInt).toArray(Integer[]::new);
        int x2 &#61; arr2[0], y2 &#61; arr2[1];

        int x3 &#61; x1 - (y1 - y2), y3 &#61; y1 &#43; (x1 - x2);
        int x4 &#61; x2 - (y1 - y2), y4 &#61; y2 &#43; (x1 - x2);

        if (set.contains(x3 &#43; &#34; &#34; &#43; y3) &amp;&amp; set.contains(x4 &#43; &#34; &#34; &#43; y4)) squareCount&#43;&#43;;

        int x5 &#61; x1 &#43; (y1 - y2), y5 &#61; y1 - (x1 - x2);
        int x6 &#61; x2 &#43; (y1 - y2), y6 &#61; y2 - (x1 - x2);
        if (set.contains(x5 &#43; &#34; &#34; &#43; y5) &amp;&amp; set.contains(x6 &#43; &#34; &#34; &#43; y6)) squareCount&#43;&#43;;
      }
    }

    return squareCount / 4;
  }
}
</code></pre> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
n &#61; int(input())
coordinates &#61; [input() for _ in range(n)]


# 算法入口
def getResult():
    squareCount &#61; 0

    coordinatesSet &#61; set(coordinates)

    for i in range(n):
        x1, y1 &#61; map(int, coordinates[i].split())
        for j in range(i &#43; 1, n):
            x2, y2 &#61; map(int, coordinates[j].split())

            x3 &#61; x1 - (y1 - y2)
            y3 &#61; y1 &#43; (x1 - x2)

            x4 &#61; x2 - (y1 - y2)
            y4 &#61; y2 &#43; (x1 - x2)

            if f&#34;{x3} {y3}&#34; in coordinatesSet and f&#34;{x4} {y4}&#34; in coordinatesSet:
                squareCount &#43;&#61; 1

            x5 &#61; x1 &#43; (y1 - y2)
            y5 &#61; y1 - (x1 - x2)

            x6 &#61; x2 &#43; (y1 - y2)
            y6 &#61; y2 - (x1 - x2)

            if f&#34;{x5} {y5}&#34; in coordinatesSet and f&#34;{x6} {y6}&#34; in coordinatesSet:
                squareCount &#43;&#61; 1

    return squareCount // 4


# 算法调用
print(getResult())
</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>